﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace FySystem.Net.FyHttp
{
    public struct FyContentTypes
    {
        public const string Application_Json = "application/json";
        public const string Application_Form_urlencoded="application/x-www-form-urlencoded";
    }

    [Obsolete("C#的HttpClient蛮好的，不需要了这个类了")]
    public class FyHttpRequest
    {
        /// <summary>
        /// URL拼接参数的方式调用调用
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string FyHttpUrlRequest(string url,FyHttpParams paras,FyHttpHeaders heads, string method,string contentType)
        {
            string res = string.Empty;
            try
            {
                if (paras != null)
                {
                    if(paras.HasParams())
                    {
                        StringBuilder sb = new StringBuilder(url);
                        for (int i = 0; i < paras.Parameters.Count; i++)
                        {
                            if (i == 0)
                                sb.Append($"?{paras.Parameters[i].Key}={paras.Parameters[i].Value}");
                            else
                                sb.Append($"&{paras.Parameters[i].Key}={paras.Parameters[i].Value}");
                        }
                        url = sb.ToString();
                    }
                }

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                //如果有头，加入头
                if (heads != null)
                {
                    if (heads.HasHeads())
                    {
                        foreach(var item in heads.Headers)
                        {
                            request.Headers.Add(item.Key, item.Value);
                        }
                    }
                }

                request.Method = method;
                request.ContentType = contentType;
                
                using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using(Stream stream = response.GetResponseStream())
                    {
                        using(StreamReader sr=new StreamReader(stream))
                        {
                            res = sr.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// 通过Data的方式调用
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="heads"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static string FyHttpDataRequest(string url,byte[] data,FyHttpHeaders heads, string method,string contentType)
        {
            string res = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                //如果有头，加入头
                if (heads != null)
                {
                    if (heads.HasHeads())
                    {
                        foreach (var item in heads.Headers)
                        {
                            request.Headers.Add(item.Key, item.Value);
                        }
                    }
                }

                request.Method = method;
                request.ContentType = contentType;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(stream))
                        {
                            res = sr.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
